/*-------------------------------------------------------------------------*/
/**
  @file		loop.c
  @author	Renaud Wanschoor
  @date	july 2004
  @version	$Revision: 2.3 $
  @brief	program that read a config file to run multiple a prtitioners repeatedly

  This program is very usefull to run a partitionners with a serie of serialized matchings
  MWM are slow to compute and reusing them is often desirable, let's suppose you have
  100 matchings in matching.1.bin .. matching.100.bin you can rerun pagrid other those matchings
  by writting a config file like this :

  1 pagrid ../graph ../grid [options] -S

  this will run pagrid with all serialize matching in the current directory
  otherwise it can be used to run several instance of pagrid, for 100 loops
  the config file could be

  100 pagrid ../graph ../grid [options]

  the config file must not contain any blank line but can contain multiple line 
  describing different jobs

  100 pagrid ../graph ../grid [options]
  100 jostle ../graph ../grid [options]
  100 metis ../graph ../grid [options]
  
*/
/*--------------------------------------------------------------------------*/

#include "plib.h"

#define INPUT_FILE "./mpiloop.cfg"
#define JOB_SIZE   100

typedef struct job{
  int size;
  char cmd[MAXLINE];  
}job;

int readcfg(char* F_in,int* my_nb_jobs,job** my_jobs);
int printjobs(job* jobs, int size);
int runcmd(char* mycmd);

int main(int argc, char *argv[]){
  int i;
  job* jobs;
  int nb_jobs = 0;  
  char F_in[MAXLINE];

  if(argc == 2){
    strcpy(F_in,argv[1]);
  }else{
    strcpy(F_in,INPUT_FILE);
  }

  readcfg(F_in,&nb_jobs,&jobs);
  /*printjobs(jobs, nb_jobs);*/
  
  for(i=0;i<nb_jobs;i++){
    printf("\nrunning : %s\n",jobs[i].cmd);
    while(jobs[i].size){
      printf("%d ",jobs[i].size);fflush(stdout);      
      runcmd(jobs[i].cmd);
      jobs[i].size--;
    }
  }

  return 0;
}

int runcmd(char* mycmd){
  int i;
  FILE* fp;
  char F_match[MAXLINE];
  char cmd[MAXLINE];
  
  if(mycmd[strlen(mycmd)-1] == 'S'){
    for(i=1;;i++){
      sprintf(F_match,"matching.%d.%s",i,IO_FSER);
      if((fp=fopen(F_match,"r")) == NULL || fclose(fp) == EOF){
	break;
      }
      sprintf(cmd,"%s %s",mycmd,F_match);
      NOTICE((system(cmd)!=0,"error running comand %s\n",cmd));
    }
    return 1;
  }

  NOTICE((system(mycmd)!=0,"error running comand %s\n",cmd));
  return 1;
}

int readcfg(char* F_in,int* my_nb_jobs,job** my_jobs){
  FILE* fp;
  char line[MAXLINE];
  int nb_jobs = 0;
  job* jobs = U_malloc(JOB_SIZE,job,"readcfg malloc error 1\n");
  int jobs_size = JOB_SIZE;
  char *tok;
  int linenb = 0;

  WARNING(((fp = fopen(F_in,"r")) == NULL,"cannot read %s\n",F_in),0);
  
  do{
    do { 
      fgets(line, MAXLINE, fp);
      linenb++;
    } while ((line[0] == '%' || strcmp(line,"\n")==0) && !feof(fp));

    if(feof(fp))
      break;

    nb_jobs++;     

    if(nb_jobs > jobs_size){
      U_realloc(jobs,jobs_size*2,job,"readcfg realloc error 1\n");
      jobs_size*=2;
    }
    
    WARNING(((tok = strtok(line," ")) == NULL,"readcfg cannot read experiments number line %d\n",linenb),0);
    
    jobs[nb_jobs-1].size = atoi(tok);

    WARNING(((tok = strtok(NULL,"\n")) == NULL,"readcfg cannot read command line %d\n",linenb),0);
        
    strcpy(jobs[nb_jobs-1].cmd,tok);
    
  }while(!feof(fp));

  fclose(fp);

  *my_jobs = jobs;
  *my_nb_jobs = nb_jobs;

  return 1;
}

int printjobs(job* jobs, int size){
  int i;
  for(i=0;i<size;i++){
    printf("job %3d\n",i);
    printf("-------\n");
    printf("cmd  : %s\n",jobs[i].cmd);    
    printf("size : %d\n\n",jobs[i].size);    
  }
  return 1;
}
